﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfDPAndAPDemo.DP
{
    public class CustomTextBox : TextBox
    {

        public bool IsHighLighted
        {
            get { return (bool)GetValue(IsHighLightedProperty); }
            set { SetValue(IsHighLightedProperty, value); }
        }


        public static readonly DependencyProperty IsHighLightedProperty = DependencyProperty.Register(
                nameof(IsHighLighted),
                typeof(bool),
                typeof(CustomTextBox),
                new PropertyMetadata(false, callback)
                );

        //回调函数
        private static void callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is CustomTextBox box)
            {
                box.TextChanged += Box_TextChanged;
            }
        }

        //事件
        private static void Box_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is CustomTextBox obj)
            {
                obj.Background = Brushes.Red;
            }
        }


        //依赖属性
        public bool HasText => (bool)GetValue(HasTextProperty);

        public static readonly DependencyProperty HasTextProperty;
        public static readonly DependencyPropertyKey HasTextPropertyKey;


        static CustomTextBox()
        {
            HasTextPropertyKey = DependencyProperty.RegisterReadOnly(
                nameof(HasText),
                typeof(bool),
                typeof(CustomTextBox),
                new PropertyMetadata(false));

            HasTextProperty = HasTextPropertyKey.DependencyProperty;
        }
    }
}
